<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <script>
    /* 
      使用场景： 
        1.多个对象互相调用的时候，通过中介者去解耦
    
    */  
    // （通过中介者去互相引用）
    // 类 A
    class A {
      constructor() {
        this.number = 0;
      }
      setNumber(num, m) {
        this.number = num;
        if(m) {
          m.setB();
        }
      }
    }
    // 类 B
    class B {
      constructor() {
        this.number = 0;
      }
      setNumber(num, m) {
        this.number = num;
        if(m) {
          m.setA();
        }
      }
    }

    // 中介者
    class Mediator {
      constructor(a,b) {
        this.a = a;
        this.b = b;
      }
      setA() {
        let number = this.b.number;
        this.a.setNumber(number / 100);
      }
      setB() {
        let number = this.a.number;
        this.b.setNumber(number * 100)
      }
    }

    // 测试
    let a = new A;
    let b = new B;
    let m = new Mediator(a,b);
    a.setNumber(100, m)
    b.setNumber(100, m)
  </script>
</body>
</html>